<?php
/**
 * @package tests
 */
require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../../../plugins/GraduaUtil/lib/test/Gradua_PHPUnit_Framework_TestCase.php';

class ServiceSchemasTest extends Gradua_PHPUnit_Framework_TestCase
{

    /**
     * @expectedException ServiceSchemaException
     * {@source}
     */
    public function testShouldRaiseExceptionIfParameterDocDifferFromParameterCount() {
      $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
	    'methodWithParamDocButNotDeclared',
	    ServicesSchemas::INPUT_SCHEMA
	  );
    }

    /**
     * {@source}
     */
    public function testShouldReturnAnnotatedInputSchema() {
      $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
	    'methodWithTestInputSchema',
	    ServicesSchemas::INPUT_SCHEMA
	  );

	  $this->assertEquals(
	    '{"type":"integer","minimum":8}',
	    json_encode($schema)
	  );
    }

    /**
     * {@source}
     */
    public function testShouldReturnAnnotatedOutputSchema() {
      $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
	    'methodWithTestOutputSchema',
	    ServicesSchemas::OUTPUT_SCHEMA
	  );

	  $this->assertEquals(
	    '{"type":"integer","minimum":8}',
	    json_encode($schema)
	  );
    }

    /**
     * @expectedException ServiceSchemaException
     * {@source}
     */
    public function testShouldDeclareOnlyOneParameter() {
      $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
	    'methodWithTwoParameters',
	    ServicesSchemas::INPUT_SCHEMA
	  );
    }

    /**
     * {@source}
     */
    public function testShouldReturnDefaultSchemaOnServiceWithoutParameter()
    {
	  $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
	    'methodWithNoParameter',
	    ServicesSchemas::INPUT_SCHEMA
	  );
	  $this->assertEquals(
	    ServicesSchemas::NO_PARAMETER_SCHEMA,
	    json_encode($schema)
	  );
    }

    /**
     * {@source}
     */
    public function testShouldReturnDefaultSchemaOnServiceWithVoidReturn()
    {
	  $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
	    'methodWithVoidReturn',
	    ServicesSchemas::OUTPUT_SCHEMA
	  );
	  $this->assertEquals(
	    ServicesSchemas::VOID_SCHEMA,
	    json_encode($schema)
	  );
    }

    /**
     * @expectedException ServiceInvalidSchemaException
     * {@source}
     */
    public function testShouldRaiseExceptionOnMalformedSchema() {
      $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
        'methodWithMalformedInputSchema',
	    ServicesSchemas::INPUT_SCHEMA
	  );
    }

    /**
     * @expectedException ServiceSchemaException
     * {@source}
     */
    public function testShouldExplicitlyDeclareReturnVoidIfNoReturn() {
      $schema = ServicesSchemas::getSchema(
	    'UnitTestsMockService',
        'methodThatDoesNotDeclaresReturnVoidOnDocs',
	    ServicesSchemas::OUTPUT_SCHEMA
	  );
    }

}